import { MutableRefObject } from 'react';
import { extendApi } from '@inbiz/utils';
import type { IProps } from './index';

export default extendApi(
  (_events: MutableRefObject<IProps['callBackEvents']>, props: IProps, filed: any) => {
    let nowValidator = filed.validator || [];
    return {
      clear: () => {
        props.onChange?.();
      },
      // 设置文本域最大字数
      setMaxLength: (len: number) => {
        let flag = false;
        filed.validator?.map((item: any, index: number) => {
          if (Object.keys(item)[0] === 'maxLength') {
            flag = true;
            filed.validator[index]['maxLength'] = len;
          }
        });
        if (!flag){
          filed.validator = filed.validator || []
          filed.validator.push({ maxLength: len });
        }
        filed.componentProps['maxLength'] = len;
      },
      // 设置文本域最小字数
      setMinLength: (len: number) => {
        let flag = false;
        filed.validator?.map((item: any, index: number) => {
          if (item?.['minLength'] !== undefined) {
            flag = true;
            filed.validator[index]['minLength'] = len;
            // nowValidator[index]['minLength'] = len;
          }
        });
        if (!flag) {
          filed.validator = filed.validator || []
          filed.validator.push({ minLength: len });
        }
        filed.componentProps['minLength'] = len;
      }
    };
  },
);
